package tool.zyk.bio;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

/**
 * author: zyk
 * datetime: 2024/1/26 23:26
 * describe:
 */
public class BioServer {
    public static void main(String[] args) {
        try {
            //1.创建服务端，监听指定端口
            ServerSocket serverSocket = new ServerSocket(8085);
            System.out.println("服务端已开启，开始监听8084端口");
            while(true){
                //2.等待客户端去连接  accept()：阻塞
                Socket client = serverSocket.accept();
                System.out.println("客户端是："+
                        client.getInetAddress().getHostAddress());
                //客户端是：192.168.11.233
                //3.数据交互，服务端->输出流
                DataOutputStream dataOutputStream = new DataOutputStream(
                        client.getOutputStream());
                dataOutputStream.writeUTF("你好，客户端！");
                TimeUnit.SECONDS.sleep(5);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
